package com.xprime.mobileFramework.function;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREInvalidObjectException;
import com.adobe.fre.FREObject;
import com.adobe.fre.FRETypeMismatchException;
import com.adobe.fre.FREWrongThreadException;

public class ItemListDialogFREFunction implements FREFunction{

	@Override
	public FREObject call(FREContext pFREContext, FREObject[] pParams) {
		final FREContext freContext = pFREContext;
		final Activity activity = pFREContext.getActivity();		
		try {
			String title = pParams[0].getAsString();
			final String[] items = pParams[1].getAsString().split(",");	
			
			new AlertDialog.Builder(activity)
			.setTitle(title)
			.setItems(items, new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int which) {	            	
	            	freContext.dispatchStatusEventAsync("itemListDialog", items[which]);
	            }
	        })
	        .create().show();
			
			return FREObject.newObject(true);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (FRETypeMismatchException e) {
			e.printStackTrace();
		} catch (FREInvalidObjectException e) {
			e.printStackTrace();
		} catch (FREWrongThreadException e) {
			e.printStackTrace();
		}		
		
		
		
		return null;
	}

}
